import json
import re
import sys
import time

import execjs
import jsonpath
import requests
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, QLineEdit, QPushButton, QVBoxLayout, QLabel, \
    QTextBrowser


class Translate(QThread):
    signal = pyqtSignal(str)

    def __init__(self, show_translate):
        super(Translate, self).__init__()
        self.show_translate = show_translate

    def text_translate(self, text):
        text = text
        url = 'https://fanyi.baidu.com/'

        headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
            "cookie": "BIDUPSID=B21B60603FC8D02F070C9DA34BE7A4AD; PSTM=1598146370; BAIDUID=B21B60603FC8D02F986921B8E271EBCF:FG=1; BDUSS=dRSHowY2NrQzF0ckh5WXBRalMyWENLT1FZRGs2MUlqVWFPREViR3RQSk0zN2xmSVFBQUFBJCQAAAAAAAAAAAEAAACbBvfFQ2FyYmxlyPAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAExSkl9MUpJfck; BDUSS_BFESS=dRSHowY2NrQzF0ckh5WXBRalMyWENLT1FZRGs2MUlqVWFPREViR3RQSk0zN2xmSVFBQUFBJCQAAAAAAAAAAAEAAACbBvfFQ2FyYmxlyPAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAExSkl9MUpJfck; Hm_lvt_afd111fa62852d1f37001d1f980b6800=1604374317; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=32811_1444_32856_33118_33060_33099_33101_32962_31709; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1604374319,1605573963; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1605602614; __yjsv5_shitong=1.0_7_71048cdd40c6001a2ca1c0f6a7a708bafb0a_300_1605602614331_49.89.253.124_8f40486a; yjs_js_security_passport=a90b0376787416b97512f503f4eb571228746428_1605602618_js"
        }

        r = requests.get(url, headers=headers)
        r.encoding = 'utf-8'
        html_text = r.text
        i = re.findall(r'window.gtk = (.*?);', html_text)[0]
        print(i)

        with open('百度翻译.js', 'r', encoding='utf-8') as f:
            js_content = f.read()
            js_content = js_content.replace("'320305.131321201'", i)

            context = execjs.compile(js_content)
            sign = context.call("e", text)

        url = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en'
        headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
            "cookie": "BIDUPSID=B21B60603FC8D02F070C9DA34BE7A4AD; PSTM=1598146370; BAIDUID=B21B60603FC8D02F986921B8E271EBCF:FG=1; BDUSS=dRSHowY2NrQzF0ckh5WXBRalMyWENLT1FZRGs2MUlqVWFPREViR3RQSk0zN2xmSVFBQUFBJCQAAAAAAAAAAAEAAACbBvfFQ2FyYmxlyPAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAExSkl9MUpJfck; BDUSS_BFESS=dRSHowY2NrQzF0ckh5WXBRalMyWENLT1FZRGs2MUlqVWFPREViR3RQSk0zN2xmSVFBQUFBJCQAAAAAAAAAAAEAAACbBvfFQ2FyYmxlyPAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAExSkl9MUpJfck; Hm_lvt_afd111fa62852d1f37001d1f980b6800=1604374317; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=32811_1444_32856_33118_33060_33099_33101_32962_31709; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1604374319,1605573963; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1605602614; __yjsv5_shitong=1.0_7_71048cdd40c6001a2ca1c0f6a7a708bafb0a_300_1605602614331_49.89.253.124_8f40486a; yjs_js_security_passport=a90b0376787416b97512f503f4eb571228746428_1605602618_js"
        }

        data = {
            'from': 'en',
            'to': 'zh',
            'query': text,
            'sign': sign,
            'token': 'dcaa69bf2e4fc027c3f7f618d7ca2942'
        }
        r = requests.post(url, headers=headers, data=data)
        # r.encoding = 'utf-8'
        ret = json.loads(r.text)
        # print(ret)
        translate_text = jsonpath.jsonpath(ret, '$..data[0]')[0]['dst']
        content = '翻译的结果: %s' % translate_text
        self.show_translate.emit(content)

    def run(self):
        while True:
            print('子线程正在执行')
            time.sleep(1)


class MainWidget(QWidget):
    show_translate = pyqtSignal(str)

    def __init__(self):
        super(MainWidget, self).__init__()
        self.init_ui()

    def init_ui(self):
        self.resize(600, 400)
        self.setWindowTitle('百度翻译')
        self.setWindowIcon(QIcon('龙.png'))
        screen = QDesktopWidget().screenGeometry()  # 获取屏幕坐标系
        size = self.geometry()  # 获取窗口坐标系
        left = (screen.width() - size.width()) / 2
        top = (screen.height() - size.height()) / 2
        self.move(left, top)
        v_layout = QVBoxLayout()
        self.text_input = QLineEdit()
        translate_btn = QPushButton('翻译')
        self.text_browser = QTextBrowser()

        v_layout.addWidget(self.text_input)
        v_layout.addWidget(translate_btn)
        v_layout.addWidget(self.text_browser)

        translate_btn.clicked.connect(self.translate)
        self.show_translate.connect(self.show_text)
        self.translate_thread = Translate(self.show_translate)
        self.translate_thread.signal.connect(self.translate_thread.text_translate)

        self.translate_thread.start()
        self.setLayout(v_layout)

    def translate(self):
        text = self.text_input.text()
        # print(text)
        self.translate_thread.signal.emit(text)

    def show_text(self, text):
        self.text_browser.setText(text)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_widget = MainWidget()
    main_widget.show()
    app.exec()
